package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.PxAdversterService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.PxAdverster;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author cwj
 * @description
 * @date 2025/4/10 10:43
 **/
@Controller
@RequestMapping("pxAdverster")
public class PxAdversterController extends BaseController {

    @Resource
    private PxAdversterService pxAdversterService;

    /**
     * @param page
     * @time: 2020/10/27 8:57
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<PxAdverster> pages = pxAdversterService.list(page);
        mv.addObject("adversters", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("adversters/px_adverster_list");
        return mv;
    }

    /**
     * @description 显示添加广告商的页面
     * @author cwj
     * @param []
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/10 17:16
     **/
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("adversters/px_adverster_add");
        return mv;
    }


    /**
     * @description 添加广告商
     * @author cwj
     * @param [adverster]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/10 17:16
     **/
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(PxAdverster pxAdverster) {
        ModelAndView mv = new ModelAndView();
        pxAdversterService.add(pxAdverster,this.getSessionUser(),this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @description 检查广告商名字是否已经存在
     * @author cwj
     * @param [id, shortName]
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/4/10 17:16
     **/
    @ResponseBody
    @RequestMapping("checkShortName")
    public Response<Object> checkName(@RequestParam(required = false) String id, String shortName) {
        PxAdverster pxAdverster = pxAdversterService.getByShortName(shortName);
        if (StringUtil.isBlank(id)) {
            if (!ObjectUtils.isEmpty(pxAdverster)) {
                return Response.failedResponse("APP标识已存在，请修改");
            }
        } else {
            if (!ObjectUtils.isEmpty(pxAdverster) && !id.equals(pxAdverster.getId())) {
                return Response.failedResponse("APP标识已存在，请修改");
            }
        }
        return Response.successResponse();
    }

    /**
     * @description 编辑广告商
     * @author cwj
     * @param [id]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 8:55
     **/
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("adverster", pxAdversterService.findById(new ObjectId(id)));
        mv.setViewName("adversters/px_adverster_edit");
        return mv;
    }

    /**
     * @description 编辑广告商
     * @author cwj
     * @param [pxAdverster]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/11 10:20
     **/
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(PxAdverster pxAdverster) {
        ModelAndView mv = new ModelAndView();
        if(ObjectUtils.isEmpty(pxAdverster) || StringUtils.isEmpty(pxAdverster.getId())){
            mv.addObject("msg", "fail");
            mv.setViewName("save_result");
            return mv;
        }
        pxAdversterService.update(pxAdverster, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

}
